home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / syscall / Fs_ReadVector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-30  |  2.8 KB  |  103 lines

  1. /* 
  2.  * Fs_ReadVector.c --
  3.  *
  4.  *    Source code for the Fs_ReadVector library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: Fs_ReadVector.c,v 1.5 88/07/29 17:08:24 ouster Exp $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20. #include <sprite.h>
  21. #include <fs.h>
  22. #include <status.h>
  23. #include <stdlib.h>
  24.  
  25. /*
  26.  *----------------------------------------------------------------------
  27.  *
  28.  * Fs_ReadVector --
  29.  *
  30.  *      The "normal" Fs_ReadVector routine for user code.  Read from the file
  31.  *      indicated by the stream ID into the buffers described in vectorArray.
  32.  *    The vectorArray indicates how much data to read, and amtReadPtr 
  33.  *    is an output parameter that indicates how much data were read.  
  34.  *    A length of zero means end-of-file.
  35.  *
  36.  *    Restarting from a signal is automatically handled by Fs_Read.
  37.  *
  38.  * Results:
  39.  *    Result from Fs_Read.
  40.  *
  41.  * Side effects:
  42.  *    See Fs_Read.
  43.  *
  44.  *----------------------------------------------------------------------
  45.  */
  46.  
  47. ReturnStatus
  48. Fs_ReadVector(streamID, numVectors, vectorArray, amtReadPtr)
  49.     int        streamID;    /* The user's index into its open file list. */
  50.     int        numVectors;    /* The # of vectors in userVectorArray. */
  51.     Fs_IOVector    vectorArray[];    /* The vectors defining where and how much to
  52.                  * read. */
  53.     int        *amtReadPtr;     /* The amount of bytes actually read. */
  54. {
  55.     register int     i;
  56.     register Fs_IOVector *vectorPtr;
  57.     register int    bufSize;
  58.     Address        buffer;
  59.     Address        ptr;
  60.     ReturnStatus    status;
  61.  
  62.     /*
  63.      * Calculate the total number of bytes to be read.
  64.      */
  65.     bufSize = 0;
  66.     for (i = 0, vectorPtr = vectorArray; i < numVectors; i++, vectorPtr++) {
  67.     if (vectorPtr->bufSize < 0) {
  68.         return SYS_INVALID_ARG;
  69.     }
  70.     bufSize += vectorPtr->bufSize;
  71.     }
  72.  
  73.     buffer = (Address) malloc((unsigned) bufSize);
  74.     status = Fs_Read(streamID, bufSize, buffer, amtReadPtr);
  75.  
  76.     if (status == SUCCESS) {
  77.     register int copyAmount;
  78.  
  79.     bufSize = *amtReadPtr;
  80.  
  81.     /*
  82.      * Copy the data to the individual buffers specified in the vectorArray.
  83.      */
  84.     ptr = buffer;
  85.     for (i = 0, vectorPtr = vectorArray;
  86.         (i < numVectors) && (bufSize > 0); 
  87.         i++, vectorPtr++) {
  88.  
  89.         if (bufSize < vectorPtr->bufSize) {
  90.         copyAmount = bufSize;
  91.         vectorPtr->bufSize = bufSize;
  92.         } else {
  93.         copyAmount = vectorPtr->bufSize;
  94.         }
  95.         bcopy(ptr, vectorPtr->buffer, copyAmount);
  96.         ptr += copyAmount;
  97.         bufSize -= copyAmount;
  98.     }
  99.     }
  100.     free((char *) buffer);
  101.     return(status);
  102. }
  103.